/**
 * <p>选择部门的查询grid</p>
 * xtype = cceafselectdeptmentgrid

 * @module Ext-ux-cceaf-control
 * @class cceaf.control.SelectDeptmentGrid
 * @extends Ext.grid.Panel
 * @since 1.0
 */
Ext.define('cceaf.control.SelectDeptmentGrid',{
	extend: 'Ext.grid.Panel', 
    alias: 'widget.cceafselectdeptmentgrid',
    requires: [
   	    'cceaf.control.TextField',
   	    'cceaf.control.TextItem',
   	    'cceaf.control.GroupPicker',
   	    'cceaf.control.Button'
   	],
       	
	constructor: function(config){
		config = config || {};
		
		Ext.define('_DeptmentModel', {
	        extend: 'Ext.data.Model',
	        fields: [
     	        {name: 'deptid'}, 
    	        {name: 'deptname'},
    	        {name: 'groupid'}, 
    	        {name: 'deptfunction'},
    	        {name: 'telphone'}, 
    	        {name: 'builddate'}, 
    	        {name: 'createtime'},
    	        {name: 'creator'}
    	   ]
	    });
	    
		//定义数据源
	    var ds = new Ext.data.Store({
	    	proxy: {
	    		type: 'ajax',
	    		url: "/" + cceaf.Global.WEB_APP_NAME + '/select/select!query.action?type=DEPTMENT',
	    		reader: {
					type: 'json',
					root: 'data',
					totalProperty: 'totalCount',
					messageProperty: 'message'
				}
			},
			
	        listeners: {
	        	'load': function() {
	        		var jr = ds.getProxy().getReader();
	        		var message = jr.jsonData.message;
	        		cceaf.util.ExtHelper.showInfoDialog(message);
	        	},
	        	'loadexception': function() {
	        		var jr = ds.getProxy().getReader();
	        		if(jr.jsonData){
		        		var message = jr.jsonData.message;
		        		cceaf.util.ExtHelper.showErrorDialog(message);
	        		}
	        		else{
	        			cceaf.util.ExtHelper.showErrorDialog("返回无效的JSONDATA数据");
	        		}
	        	}
	        },
	        
	        autoDestroy: true,  
	    	pageSize: szpshang.Global.PAGE_SIZE,
	    	model: '_DeptmentModel'
	    });
	    
	    //列序号模式
	    //var nm = new Ext.grid.RowNumberer();
	    //多选列模式
		//var selModel = new Ext.selection.CheckboxModel();
	    var selModel = new Ext.selection.RowModel();
	    
		//查询对象
	    function btnQuery_click(){
	    	var deptmentGrid = this.up('gridpanel');
	    	
	    	var groupid = deptmentGrid.down("[name=q_deptment_groupid]").getValue();
	    	var deptname = deptmentGrid.down("[name=q_deptment_deptname]").getValue();
	    	
	    	ds.proxy.extraParams = {
	    		groupid:groupid,
	    		deptname:deptname
	    	};
	    	
	    	//ds.setBaseParam("groupid",groupid);
	    	//ds.setBaseParam("deptname",deptname);
	    	
	    	ds.load({params:{isquery:true}});
	    }
	    
	  	//重置查询条件
	    function btnReset_click(){
	    	var deptmentGrid = this.up('gridpanel');
	    	
	    	deptmentGrid.down("[name=q_deptment_groupid]").setValue("");
	    	deptmentGrid.down("[name=q_deptment_deptname]").setValue("");
	    }
	    
	 	//定义表格列
	    var columns = [
	    	{xtype: 'rownumberer'},
	        {dataIndex: 'deptid',        header: '部门编号',         sortable: true,  hidden: true}, 
	        {dataIndex: 'deptname',      header: '部门名称',         sortable: true, flex: 1},
	        {dataIndex: 'groupid',       header: '组织名称',         sortable: true, flex: 1, renderer: cceaf.Global.cceafGroupRenderer},
	        {dataIndex: 'deptfunction',  header: '部门职能',         sortable: true, flex: 3},
	        {dataIndex: 'telphone',      header: '部门联系电话',     sortable: true, flex: 1}, 
	        {dataIndex: 'builddate',     header: '成立日期',         sortable: true, flex: 1}
	        //{dataIndex: 'createtime',    header: '创建时间',      width:20,   sortable: true},
	        //{dataIndex: 'creator',       header: '创建人',        width:20,   sortable: true}
	    ];
	    
		Ext.applyIf(config, {
			//id:'_selectDeptmentGrid',
	    	title:'部门信息',
	        border: true,
	        frame: true,
	        //height: cceaf.util.ExtHelper.getBodyHeight(),
	        //width: document.body.scrollWidth,
	        store: ds,
	        //sm:sm,
	        selModel: selModel,
	        columns: columns,
	        loadMask: {msg:'正在加载数据，请稍侯……'},
	        
	        viewConfig: {
				forceFit:true,
				enableRowBody:true,
				getRowClass : function(record, rowIndex, p, ds){
					return '';
				}
			},
			
			dockedItems:[{
				xtype : 'toolbar',  
                dock : 'top',  
                items : [
					'组织机构',
					{xtype: 'cceafgrouppicker', name: 'q_deptment_groupid'},
					{xtype: 'cceaftbtext', text: ''},
					'部门名称',
					{xtype: 'cceaftextfield', name: 'q_deptment_deptname'},
					{xtype: 'cceaftbtext', text: ''},
					{xtype: "tbfill"},
					{iconCls: 'page_queryIcon', text: ' 查 询 ', xtype: 'cceafbutton', handler: btnQuery_click},
					'-',
					{iconCls: 'page_resetIcon', text: ' 重 置 ', xtype: 'cceafbutton', handler: btnReset_click}
				]
			},{
				xtype: 'pagingtoolbar',
				store: ds,
                dock: 'bottom',  
                displayInfo: true,
                displayMsg:'当前显示 {0} - {1}条记录 /共 {2}条记录',
	            emptyMsg:'无显示数据'
			}],
	        
			listeners: {
	        	'render': function() {
	        		//tbarButton.render(queryGrid.tbar);
	        	},
	        	'itemdblclick': function() {
	        		this.selectRow();
	        	},
	        	'keydown': function(e){
	        		if(e.keyCode == 13 && this.getSelectionModel().getLastSelected() != null){
	        			this.selectRow();
	        		}
	        	}
	        },
	        
	        selectRow: function(){
	        	var selectDeptmentGrid = this;
	        	
	        	//获取部门字段的父对象
	        	var deptmentFieldFather = this.deptmentField.up();
	        	
        		//填充数据到父窗口的控件
        		if(selectDeptmentGrid.nameOfDeptid != null && selectDeptmentGrid.nameOfDeptid != ""){
        			deptmentFieldFather.down('[name=' + selectDeptmentGrid.nameOfDeptid + ']').setValue(this.getSelectionModel().getLastSelected().get("deptid"));
        		}
        		selectDeptmentGrid.deptmentField.setValue(this.getSelectionModel().getLastSelected().get("deptname"));
        		
        		var selectGroupWin = Ext.getCmp('_selectDeptmentWin');
		    	if(selectGroupWin){
		    		selectGroupWin.hide();
		    	}
	        }
	        
		});
		
		cceaf.control.SelectDeptmentGrid.superclass.constructor.apply(this, arguments);
	},
	
	/**
	 * 部门ID控件的name
	 *
     * @property nameOfDeptid 
     * @type String
     * @default null
     */
	nameOfDeptid: null,
	
	/**
	 * 部门名称控件
	 *
     * @property nameOfDeptid 
     * @type cceafdeptmentfield
     * @default null
     */
	deptmentField: null
	
});